import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {tableConfig, header} from '../data/config'
import store from 'store'

const base = url.base

let table,
  bankList

//初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base + '/admin/bankProtocol/facade.json',
  pageNumber: 1,
  pageSize: 10,
  queryParams: function (params) {
    return $.extend({
      currentPage: params.pageNumber,
      pageSize: params.pageSize
    }, $('#searchForm').serializeObject())
  }, ajax: function (params) {  //  自定义ajax
    $.ajax({
      url: params.url,
      type: params.type,
      contentType: params.contentType,
      headers: {
        myToken: Cookies.get('myToken')
      },
      data: params.data
    })
      .done(function (res) {
        bankList = res.bankInfoList
        bankList.forEach(k => {
          let bankId = k.channelCode
          let payChannel = k.payChannel
          let str = `<option name="bankId" value="${bankId}">${payChannel}</option>`
          $('#openStatusSearch').append(str)
        })
        params.success({
          total: res.object.totalRows,
          rows: res.object instanceof Array ? res.object : res.object.elements
        })
      })
  },
  columns: [
    {
      field: 'attribute',
      title: '银行名称',
      valign: 'middle',
      formatter: (index, row) => {
        return row.attribute
      }
    },
    {
      field: 'protocolName',
      title: '协议名称',
      valign: 'middle'
    },
    {
      field: 'rawAddTime',
      title: '创建时间',
      valign: 'middle',
      formatter: (index, row) => {
        let date = new Date(row.rawAddTime)
        return date.toLocaleDateString() + ' ' + date.toLocaleTimeString()
      }
    }
    ,
    {
      field: 'hello',
      valign: 'middle',
      align: 'center',
      title: '操作',
      formatter: (index, row) => {
        return `<div class="btn-group">
                  <button type="button" data-id="${ row.id }" class="btn btn-default btn-detail">详情</button>
                  <button type="button" data-id="${ row.id }" class="btn btn-default btn-modify">修改</button>
                </div>`
      }
    }
  ]
}))

$(document).on('click', '#table .btn-detail', e => {
  let url = $(e.target).attr('data-id')
  location.href = `./edit.html?id=${url}&justLook=1`
})

$(document).on('click', '#table .btn-modify', e => {
  let url = $(e.target).attr('data-id')
  location.href = `./edit.html?id=${url}`
})

$(document).on('click', '.toolbar .btn-search', () => {
  table.bootstrapTable('refresh')
})

